package com.itfenghuang.day15_jiaoshiwenj;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 多人聊天服务端
 */
public class A05_ServerChat {
    //存储socket对象
    private static ArrayList<Socket> sockets=new ArrayList<>();
    //存储接收到的消息
    private static LinkedList<String> message=new LinkedList<>();

    public static void main(String[] args) throws IOException {
        /**
         * 1.接收客户端的消息,并将消息回送给其他的客户端
         */
        ServerSocket ss = new ServerSocket(10086);
        /**
         * 监听连接的线程
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        //监听连接,因为是多人聊天,每连接一个人都会生成一个Socket
                        Socket socket = ss.accept();
                        System.out.println("有人上线了");
                        //将socket对象存到集合中
                        sockets.add(socket);
                        //线程,接收客户端发送的消息
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                //接收客户端的消息,循环接收
                                try {
                                    InputStream is = socket.getInputStream();
                                    System.out.println(socket.toString());
                                    //创建 字符缓冲输入流
                                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                                    //循环读取
                                    while(true){
                                        //一次读取一行
                                        String line = br.readLine();
                                        System.out.println(line);
                                       // message.addFirst(socket.toString()+"=="+line);
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
        //将消息回送给客户端的线程
        new Thread(new Runnable() {
            @Override
            public void run() {
               while(true){
                   try {
                       Thread.sleep(100);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   if(message.isEmpty()){

                   }else{
                      //获取集合中的元素
                       //String last = message.getLast();
                       String last = message.removeLast();
                       //发送给所有的客户端用户
                       for (Socket socket : sockets) {
                           //获取输出流
                           try {
                               OutputStream os = socket.getOutputStream();
                               //创建字符缓冲输出流
                               BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
                               bw.write(last);
                               bw.newLine();
                               bw.flush();
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }
                   }
               }
            }
        }).start();
    }
}
